<?php

namespace app\api\controller;

use app\admin\model\Crop AS CropMdl;
use app\admin\model\Croptype;
use app\admin\model\Varieties;

class Crop extends BaseController
{

    /**
     * 获取农作物分类
     */
    public function getCroptypes()
    {
        $param = self::getHttpParam();

        $where = [];
        $model = new Croptype();
        $fields = 'id, name, imgUrl';
        $order = 'sorting ASC';
        $result = $model->where($where)->field($fields)->order($order)->select();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关农作物分类');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

    /**
     * 获取农作物
     */
    public function getCrops()
    {
        $param = self::getHttpParam();

        if (empty($param->typeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'typeId 不能为空');
        }
        $where = [
            'typeId' => $param->typeId,
        ];

        $model = new CropMdl();
        $fields = 'id, name, imgUrl';
        $order = 'sorting ASC';
        $result = $model->where($where)->field($fields)->order($order)->select();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关农作物');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

    /**
     * 获取品种
     */
    public function getVarieties()
    {
        $param = self::getHttpParam();

        if (empty($param->cropId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'cropId 不能为空');
        }
        $where = [
            'cropId' => $param->cropId,
        ];

        $model = new Varieties();
        $fields = 'id, name, imgUrl';
        $order = 'sorting ASC';
        $result = $model->where($where)->field($fields)->order($order)->select();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关品种');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

}